from kivy.logger import Logger
from kivy.uix.checkbox import CheckBox
from kivy.uix.boxlayout import BoxLayout

from model.gamestate import GameState, Players, Hat, Phase,\
    WordGuessResult, GameTypes, GameType

from ui.hat_widget import HatWidget
from ui.fittable_widgets import FittableLabel


class NextRound(HatWidget):
    """class for widget with options for next round"""
    PROPS = {}
    AVALIABLE_PROPS = {Phase.NEXT_ROUND: ('game_type', 'keep_failed_words')}

    def __init__(self, gamestate, presenter, **kwargs):
        super(NextRound, self).__init__(**kwargs)

        self.presenter = presenter
        self.gamestate = gamestate

        self.next_game_type = gamestate.game_type.type\
            if (gamestate is not None and
                gamestate.game_type is not None)\
            else GameTypes.HAT_STANDARD
        self.keep_failed_words =\
            presenter.settings.next_round.keep_failed_words

        for game_type in GameTypes.text_repr:
            label = FittableLabel(id='label', group='labels',
                                  text=_(GameTypes.text_repr[game_type]))
            radio_button = CheckBox(id='radiobutton', group='game_types')
            radio_button.active = (game_type == self.next_game_type)

            field = BoxLayout(orientation='horizontal')
            field.add_widget(label)
            field.add_widget(radio_button)
            self.game_types_list.add_widget(field)

    def _set_next_game_type_from_text(self, text):
        self.next_game_type = GameType(GameTypes.HAT_STANDARD)
        for type in GameTypes.text_repr:
            if GameTypes.text_repr[type] == text:
                self.next_game_type = GameType(type)

    def set_next_game_type(self):
        for child in self.game_types_list.children:
            rbutton = None
            label = None
            for elem in child.children:
                if elem.id == 'radiobutton':
                    rbutton = elem
                elif elem.id == 'label':
                    label = elem
            if rbutton is not None and rbutton.active and label is not None:
                text = label.text
                self._set_next_game_type_from_text(text)
                break

    def _return_words_to_hat(self):
        allowed_results = [WordGuessResult.GUESS,
                           WordGuessResult.PUT_BACK,
                           WordGuessResult.TIME_OUT]
        if self.keep_failed_words:
            allowed_results.append(WordGuessResult.FAIL)

        hat = Hat([])
        for rnd in self.gamestate.rounds:
            if rnd.is_replayed:
                continue
            for guess in rnd.words:
                if guess.result in allowed_results:
                    hat.add_word(guess.word)
        return hat

    def next_view(self):
        self.gamestate.players.players_select_strategy.clear_pair()
        players = Players(self.gamestate.players.players,
                          self.gamestate.players.players_select_strategy)
        hat = self._return_words_to_hat()
        game_round = None
        phase = Phase.NEXT_ROUND
        rounds = None
        current_added_player = None
        settings = self.gamestate.settings
        self.set_next_game_type()

        self.presenter.game_state = GameState(players, hat, game_round, phase,
                                              rounds, current_added_player,
                                              settings, self.next_game_type)
        self.presenter.change_phase(Phase.FROM_TO)

    def go_back(self):
        self.presenter.change_phase(Phase.GAME_RESULTS)
